<?php

namespace app\data\controller\index;

use think\admin\Controller;

/**
 * 控制台
 * Class Portal
 * @package app\data\controller\total
 */
class Portal extends Controller
{
    /**
     * 数据报表
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->retrievalTotal = $this->app->db->name('sn_goods_retrieval')->cache(true, 60)->where('status',4)->sum('retrieval_num');
        $this->stockTotal = $this->app->db->name('sn_goods_item')->cache(true, 60)->sum('stock_num');
        $this->orderTotal = $this->app->db->name('sn_goods_item')->cache(true, 60)->sum('stock_total');
        $this->sendTotal = $this->app->db->name('sn_goods_retrieval')->cache(true, 60)->where('status',4)->sum('retrieval_total');
        // 近一个月用户及交易趋势
        $this->days = $this->app->cache->get('portals', []);
        if (empty($this->days)) {
            for ( $i = 0  ;$i < date("t");  $i++) {
                $date = date('Y-m-d', strtotime("+{$i}days"));
                $this->days[] = [
                    '当天日期' => date('m-d', strtotime("+{$i}days")),
                    '入库总价' => $this->app->db->name('sn_goods_item')->whereLike('create_at', "{$date}%")->sum('material_total'),
                    '出库总价' => $this->app->db->name('sn_goods_retrieval')->whereLike('create_at', "{$date}%")->whereRaw('deleted=0 and status>=4')->sum('retrieval_total'),
                ];
            }
            $this->app->cache->set('portals', $this->days, 60);
        }
        $this->fetch();
    }

}